﻿using SD.Common.PoweredByLee;
using SD.ContentSystem.AppService.Maps;
using SD.ContentSystem.Domain.Entities;
using SD.ContentSystem.Domain.IRepositories;
using SD.ContentSystem.Domain.Mediators;
using SD.ContentSystem.IAppService.DTOs.Outputs;
using SD.ContentSystem.IAppService.Interfaces;
using SD.Infrastructure.DTOBase;
using SD.Infrastructure.Global.Transaction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace SD.ContentSystem.AppService.Implements
{
    /// <summary>
    /// 链接服务契约实现
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class LinkContract : ILinkContract
    {
        #region # 字段及依赖注入构造器

        /// <summary>
        /// 领域服务中介者
        /// </summary>
        private readonly DomainServiceMediator _svcMediator;

        /// <summary>
        /// 仓储中介者
        /// </summary>
        private readonly RepositoryMediator _repMediator;

        /// <summary>
        /// 单元事务
        /// </summary>
        private readonly IUnitOfWorkCMS _unitOfWork;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        /// <param name="svcMediator">领域服务中介者</param>
        /// <param name="repMediator">仓储中介者</param>
        /// <param name="unitOfWork">单元事务</param>
        public LinkContract(DomainServiceMediator svcMediator, RepositoryMediator repMediator,
            IUnitOfWorkCMS unitOfWork)
        {
            this._svcMediator = svcMediator;
            this._repMediator = repMediator;
            this._unitOfWork = unitOfWork;
        }

        #endregion


        //命令部分

        #region # 创建链接类别 —— void CreateLinkCategory(string categoryNo, string categoryName...
        /// <summary>
        /// 创建链接类别
        /// </summary>
        /// <param name="categoryNo">链接类别编号</param>
        /// <param name="categoryName">链接类别名称</param>
        /// <param name="sort">排序</param>
        public void CreateLinkCategory(string categoryNo, string categoryName, int sort)
        {
            //验证
            Assert.IsFalse(this._repMediator.LinkCategoryRep.Exists(categoryNo), "链接类别编号已存在！");

            LinkCategory linkCategory = new LinkCategory(categoryNo, categoryName, sort);

            this._unitOfWork.RegisterAdd(linkCategory);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 修改链接类别 —— void UpdateLinkCategory(Guid categoryId, string categoryName...
        /// <summary>
        /// 修改链接类别
        /// </summary>
        /// <param name="categoryId">链接类别Id</param>
        /// <param name="categoryName">链接类别名称</param>
        /// <param name="sort">排序</param>
        public void UpdateLinkCategory(Guid categoryId, string categoryName, int sort)
        {
            LinkCategory currentCategory = this._unitOfWork.Resolve<LinkCategory>(categoryId);

            currentCategory.UpdateInfo(categoryName, sort);

            this._unitOfWork.RegisterSave(currentCategory);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 删除链接类别 —— void RemoveLinkCategory(Guid categoryId)
        /// <summary>
        /// 删除链接类别
        /// </summary>
        /// <param name="categoryId">链接类别Id</param>
        public void RemoveLinkCategory(Guid categoryId)
        {
            LinkCategory currentCategory = this._unitOfWork.Resolve<LinkCategory>(categoryId);

            //验证
            Assert.IsTrue(this._repMediator.LinkRep.ExistsByCategory(currentCategory.Number), "链接类别下存在链接，不可删除！");

            this._unitOfWork.RegisterPhysicsRemove<LinkCategory>(currentCategory);
            this._unitOfWork.UnitedCommit();
        }
        #endregion


        #region # 创建链接 —— void CreateLink(string linkCategoryNo, string linkName...
        /// <summary>
        /// 创建链接
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <param name="linkName">链接名称</param>
        /// <param name="url">链接地址</param>
        /// <param name="imagePath">图片路径</param>
        /// <param name="description">描述</param>
        public void CreateLink(string linkCategoryNo, string linkName, string url, string imagePath, string description)
        {
            //验证
            Assert.IsTrue(this._repMediator.LinkCategoryRep.Exists(linkCategoryNo), "链接类别编号不存在！");

            Link link = new Link(linkCategoryNo, linkName, url, imagePath, description);

            this._unitOfWork.RegisterAdd(link);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 修改链接 —— void UpdateLink(Guid linkId, string linkName, string url...
        /// <summary>
        /// 修改链接
        /// </summary>
        /// <param name="linkId">链接Id</param>
        /// <param name="linkName">链接名称</param>
        /// <param name="url">链接地址</param>
        /// <param name="imagePath">图片路径</param>
        /// <param name="description">描述</param>
        public void UpdateLink(Guid linkId, string linkName, string url, string imagePath, string description)
        {
            Link currentLink = this._unitOfWork.Resolve<Link>(linkId);
            currentLink.UpdateInfo(linkName, url, imagePath, description);

            this._unitOfWork.RegisterSave(currentLink);
            this._unitOfWork.UnitedCommit();
        }
        #endregion

        #region # 删除链接 —— void RemoveLink(Guid linkId)
        /// <summary>
        /// 删除链接
        /// </summary>
        /// <param name="linkId">链接Id</param>
        public void RemoveLink(Guid linkId)
        {
            this._unitOfWork.RegisterPhysicsRemove<Link>(linkId);
            this._unitOfWork.Commit();
        }
        #endregion


        //查询部分

        #region # 获取链接类别 —— LinkCategoryInfo GetLinkCategory(Guid linkCategoryId)
        /// <summary>
        /// 获取链接类别
        /// </summary>
        /// <param name="linkCategoryId">链接类别Id</param>
        /// <returns>链接类别</returns>
        public LinkCategoryInfo GetLinkCategory(Guid linkCategoryId)
        {
            LinkCategory currentCategory = this._repMediator.LinkCategoryRep.Single(linkCategoryId);
            LinkCategoryInfo currentCategoryInfo = currentCategory.ToDTO();

            return currentCategoryInfo;
        }
        #endregion

        #region # 获取链接类别 —— LinkCategoryInfo GetLinkCategoryByNo(string linkCategoryNo)
        /// <summary>
        /// 获取链接类别
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <returns>链接类别</returns>
        public LinkCategoryInfo GetLinkCategoryByNo(string linkCategoryNo)
        {
            LinkCategory currentCategory = this._repMediator.LinkCategoryRep.Single(linkCategoryNo);
            LinkCategoryInfo currentCategoryInfo = currentCategory.ToDTO();

            return currentCategoryInfo;
        }
        #endregion

        #region # 获取链接类别列表 —— IEnumerable<LinkCategoryInfo> GetLinkCategories()
        /// <summary>
        /// 获取链接类别列表
        /// </summary>
        /// <returns>链接类别列表</returns>
        public IEnumerable<LinkCategoryInfo> GetLinkCategories()
        {
            IEnumerable<LinkCategory> linkCategories = this._repMediator.LinkCategoryRep.FindAll().OrderBy(x => x.Sort);
            IEnumerable<LinkCategoryInfo> linkCategoryInfos = linkCategories.Select(x => x.ToDTO());

            return linkCategoryInfos;
        }
        #endregion

        #region # 分页获取链接类别列表 —— PageModel<LinkCategoryInfo> GetLinkCategoriesByPage(...
        /// <summary>
        /// 分页获取链接类别列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>链接类别列表</returns>
        public PageModel<LinkCategoryInfo> GetLinkCategoriesByPage(string keywords, int pageIndex, int pageSize)
        {
            int rowCount, pageCount;

            IEnumerable<LinkCategory> linkCategories = this._repMediator.LinkCategoryRep.FindByPage(keywords, pageIndex, pageSize, out rowCount, out pageCount);
            IEnumerable<LinkCategoryInfo> linkCategoryInfos = linkCategories.Select(x => x.ToDTO());

            return new PageModel<LinkCategoryInfo>(linkCategoryInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion


        #region # 获取链接 —— LinkInfo GetLink(Guid linkId)
        /// <summary>
        /// 获取链接
        /// </summary>
        /// <param name="linkId">链接Id</param>
        /// <returns>链接</returns>
        public LinkInfo GetLink(Guid linkId)
        {
            Link currentLink = this._repMediator.LinkRep.Single(linkId);
            LinkInfo currentLinkInfo = currentLink.ToDTO();

            return currentLinkInfo;
        }
        #endregion

        #region # 分页获取链接列表 —— PageModel<LinkInfo> GetLinksByPage(string linkCategoryNo...
        /// <summary>
        /// 分页获取链接列表
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>链接列表</returns>
        public PageModel<LinkInfo> GetLinksByPage(string linkCategoryNo, int pageIndex, int pageSize)
        {
            int rowCount, pageCount;

            IEnumerable<Link> links = this._repMediator.LinkRep.FindByCategory(linkCategoryNo, pageIndex, pageSize, out rowCount, out pageCount);
            IEnumerable<LinkInfo> linkInfos = links.Select(x => x.ToDTO());

            return new PageModel<LinkInfo>(linkInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 根据链接类别获取链接列表 —— IEnumerable<LinkInfo> GetLinksByCategory(string...
        /// <summary>
        /// 根据链接类别获取链接列表
        /// </summary>
        /// <param name="linkCategoryNo">链接类别编号</param>
        /// <returns>链接列表</returns>
        public IEnumerable<LinkInfo> GetLinksByCategory(string linkCategoryNo)
        {
            IEnumerable<Link> links = this._repMediator.LinkRep.FindByCategory(linkCategoryNo);
            IEnumerable<LinkInfo> linkInfos = links.Select(x => x.ToDTO());

            return linkInfos;
        }
        #endregion
    }
}
